package com.qckj.amz.controller;

import com.qckj.amz.domain.ProductInit;
import com.qckj.amz.service.IProductInitService;
import com.qckj.common.annotation.Log;
import com.qckj.common.core.controller.BaseController;
import com.qckj.common.core.domain.AjaxResult;
import com.qckj.common.core.page.TableDataInfo;
import com.qckj.common.enums.BusinessType;
import com.qckj.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 初始化商品Controller
 *
 * @author luchuan
 * @since 1.0 2021-12-17
 */
@RestController
@RequestMapping("/amz/productInit")
public class ProductInitController extends BaseController {
    @Autowired
    private IProductInitService productInitService;

    /**
     * 查询初始化商品列表
     */
    @PreAuthorize("@ss.hasPermi('amz:productInit:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductInit productInit) {
        Page<ProductInit> page = productInitService.findProductInitPage(productInit);
        return getDataTable(page);
    }

    /**
     * 导出初始化商品列表
     */
    @PreAuthorize("@ss.hasPermi('amz:productInit:export')")
    @Log(title = "初始化商品", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ProductInit productInit) {
        List<ProductInit> list = productInitService.findProductInitList(productInit);
        ExcelUtil<ProductInit> util = new ExcelUtil<>(ProductInit.class);
        return util.exportExcel(list, "ProductInit");
    }

    /**
     * 获取初始化商品详细信息
     */
    @PreAuthorize("@ss.hasPermi('amz:productInit:query')")
    @GetMapping(value = "/{productInitId}")
    public AjaxResult getInfo(@PathVariable("productInitId") Long productInitId) {
        return AjaxResult.success(productInitService.findById(productInitId));
    }

    /**
     * 新增初始化商品
     */
    @PreAuthorize("@ss.hasPermi('amz:productInit:add')")
    @Log(title = "初始化商品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductInit productInit) {
        productInitService.save(productInit);
        return AjaxResult.success();
    }

    /**
     * 修改初始化商品
     */
    @PreAuthorize("@ss.hasPermi('amz:productInit:edit')")
    @Log(title = "初始化商品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductInit productInit) {
        productInitService.save(productInit);
        return AjaxResult.success();
    }

    /**
     * 删除初始化商品
     */
    @PreAuthorize("@ss.hasPermi('amz:productInit:remove')")
    @Log(title = "初始化商品", businessType = BusinessType.DELETE)
	@DeleteMapping("/{productInitIds}")
    public AjaxResult remove(@PathVariable Long[] productInitIds) {
        productInitService.deleteByIds(Arrays.asList(productInitIds));
        return AjaxResult.success();
    }


    /**
     * 修改是否默认状态
     * @param productInitId
     * @param qcIsDefault
     * @return
     */
    @RequestMapping(value = "/qcIsDefaultChange", method = RequestMethod.POST)
    public AjaxResult export(@RequestParam(name = "productInitId") String productInitId,
                             @RequestParam(name = "qcIsDefault") String qcIsDefault) {
        AjaxResult res= productInitService.qcIsDefaultChange(productInitId,qcIsDefault);
        return res;
    }

    /**
     * 导出时通过当前用户获取常用属性
     * @return
     */
    @GetMapping("/productInitByCreateBy")
    public AjaxResult productInitByCreateBy() {
        AjaxResult res= productInitService.productInitByCreateBy();
        return res;
    }

}
